package com.D5Power.Editor.EditorWindow
{
	import com.D5Power.Editor.tools.LineShower;
	import com.adobe.images.PNGEncoder;
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.FileListEvent;
	import flash.events.TimerEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.geom.Point;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	
	import org.aswing.FlowLayout;
	import org.aswing.JButton;
	import org.aswing.JLabel;
	import org.aswing.JPanel;
	import org.aswing.JTextField;
	import org.aswing.ext.Form;

	public class CharacterLinker extends D5EditorWin
	{
		private var lab:JLabel;
		private var bt_browser:JButton;
		private var bt_make:JButton;
		private var lab_size:JTextField;
		private var _shower:JPanel;
		private var DataList:Array;
		private var buffer:BitmapData;
		
		private var blockw:uint;
		private var blockh:uint;
		
		private var fill_line:uint;
		
		private var lastPath:String='';
		
		public static var padding:uint = 5;
		public static var h:uint = 20;
		public static var allowList:Array = ['jpg','png','gif'];
		public static var setSized:Boolean=false;
		
		public function CharacterLinker(owner:D5RpgEditor)
		{
			mytitle = '角色合并';
			super(owner);
		}
		
		public function get form():Form
		{
			return _box;
		}
		
		public function get shower():JPanel
		{
			return _shower;
		}
		
		override protected function init(mode:uint=0):void
		{
			
			_box = new Form();
			
			lab = new JLabel('添加：');
			lab_size = new JTextField('',9);
			bt_browser = new JButton('浏览') //(this,lab_size.x+lab_size.width+padding,padding,,addNewAction);
			bt_make = new JButton('生成'); //(this,bt_browser.x+bt_browser.width+padding,padding,'生成',onSave);
			
			_box.addRow(lab,lab_size,bt_browser,bt_make);
			bt_browser.addActionListener(addNewAction);
			bt_make.addActionListener(onSave);
			
			
			_shower = new JPanel(new FlowLayout(FlowLayout.LEFT));
			_shower.setSizeWH(200,380);
			_box.append(_shower);
			
			_box.setSizeWH(200,400);
			getContentPane().appendAll(_box);
			
			
			
			setSized=false;
			super.init(D5EditorWin.NONE);
		}
		
		public function setImgSize(w:uint,h:uint):void
		{
			lab_size.setText(w+","+h);
			setSized=true;
			
		}
		
		public function get unsetImgSize():Boolean
		{
			return lab_size.getText()=='';
		}
		
		protected function addNewAction(e:Event):void
		{
			var path:String = lastPath=='' ? File.desktopDirectory.resolvePath(lastPath).nativePath : lastPath;
			var f:File = new File(path);
			f.addEventListener(Event.SELECT,onOpen);
			f.browseForDirectory('请选择一个目录');
		}
		
		protected function onOpen(e:Event):void
		{
			var target:File = e.target as File;
			var list:Array = target.getDirectoryListing();
			var hasImg:Boolean=false;
			var checker:String;
			
			var path:String = target.nativePath;
			//var end:uint = path.lastIndexOf('\\');
			//path = path.substr(0,end);
			lastPath = path; // 记录最后一次打开的目录
			
			
			for each(var o:File in list)
			{
				checker = o.name.substr(o.name.length-3);
				if(allowList.indexOf(checker)!=-1)
				{
					hasImg = true;
					break;
				}
			}
			
			if(hasImg)
			{
				if(DataList==null) DataList = new Array();
				var line:LineShower = new LineShower(DataList.length,this);
				line.load(list);
				DataList.push(line);
			}
		}
		
		protected function onSave(e:Event):void
		{
			if(DataList.length==0 || DataList[0].Data.length==0) return;
			if(lab_size.getText()=='') return;
			
			var size:Array = lab_size.getText().split(',');
			if(size.length!=2) return;
			
			blockw = int(size[0]);
			blockh = int(size[1]);
			fill_line = 0;
			
			if(buffer!=null) buffer.dispose();
			
			var b:BitmapData = new BitmapData(DataList[0].Data.length*blockw,DataList.length*blockh,true,0x00000000);
			buffer = b;
			
			var timer:Timer = new Timer(20);
			timer.addEventListener(TimerEvent.TIMER,MakeData);
			timer.start();
		}
		
		protected function MakeData(e:TimerEvent):void
		{
			var line:LineShower = DataList.shift() as LineShower
			var fill_row:uint = 0;
			for each(var d:BitmapData in line.Data)
			{
				buffer.copyPixels(d,d.rect,new Point(fill_row*blockw+(blockw-d.width)*.5,fill_line*blockh+(blockh-d.height)),null,null,true);
				fill_row++;
			}
			fill_line++;
			
			while(line.Data.length)
			{
				(line.Data[0] as BitmapData).dispose();
				line.Data.shift();
			}
			
			_shower.removeAll();
			line=null;
			
			
			if(DataList.length==0)
			{
				var target:Timer = e.target as Timer;
				target.stop();
				target.removeEventListener(TimerEvent.TIMER,MakeData);
				
				var f:File = File.applicationDirectory.resolvePath('');
				f.browseForSave('保存文件');
				f.addEventListener(Event.SELECT,onSaveAs);
			}
		}
		
		protected function onSaveAs(e:Event):void
		{
			var f:File = e.target as File;
			
			var bytes:ByteArray = new ByteArray;
			bytes = PNGEncoder.encode(buffer);
			
			var fs:FileStream = new FileStream();
			fs.open(f,FileMode.WRITE);
			fs.writeBytes(bytes,0,bytes.bytesAvailable);
			fs.close();
			
		}
	}
}